package com.ninetaildemonfox.zdl.socialproject.utils.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.ninetaildemonfox.utils.FlowLayout;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.FlowContract;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.presenter.FlowPresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/16
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */
public class FlowDialog extends BaseActivity implements FlowContract {

    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_title)
    TextView text_title;
    @BindView(R.id.text_parent)
    TextView textParent;
    @BindView(R.id.fl_context)
    FrameLayout flContext;
    @BindView(R.id.flowLayout)
    FlowLayout flowLayout;
    private FlowPresenter presenter;
    private LinearLayout.LayoutParams layoutParams;
    private String member_id;
    private String thiscount;
    private List<Map<String, String>> data;
    private String sex;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_flow;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        data = new ArrayList<>();
        member_id = getIntent().getExtras().getString("member_id");
        thiscount = getIntent().getExtras().getString("count");
        if (getIntent().getExtras().getString("sex") != null) {
            sex = getIntent().getExtras().getString("sex");
        }
        if (!TextUtils.isEmpty(sex)) {
            if (sex.equals("1")) {
                text_title.setText("他的真实评价");
            } else {
                text_title.setText("她的真实评价");
            }
        }
        presenter = new FlowPresenter(me, this);
        presenter.commentList(token, member_id);
        //往容器内添加TextView数据
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 5, 10, 5);
    }

    private void initEvents(final TextView textView, final int count) {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (thiscount.equals("2")) {
                    Map<String, String> map = data.get(count);
                    if (map.get("is_comment").equals("2")) {
                        map.put("is_comment", "1");
                        textView.setBackgroundResource(R.drawable.drawable_flow_false);
                    } else {
                        map.put("is_comment", "2");
                        textView.setBackgroundResource(R.drawable.drawable_flow_true);
                    }
                }
            }
        });
    }

    @Override
    public void flowSuccess(List<Map<String, String>> cccc) {
        if (flowLayout != null) {
            flowLayout.removeAllViews();
        }
        data.clear();
        data.addAll(cccc);
        for (int i = 0; i < data.size(); i++) {
            TextView tv = new TextView(this);
            tv.setPadding(30, 20, 30, 20);
            String label_name = data.get(i).get("label_name") + "(" + data.get(i).get("count") + ")";
            tv.setText(label_name);
            tv.setMaxEms(10);
            tv.setSingleLine();

            if (TextUtils.isEmpty(sex)) {
                String count = data.get(i).get("count");
                if (!TextUtils.isEmpty(count)) {
                    Integer integer = Integer.valueOf(count);
                    if (integer == 0) {
                        tv.setBackgroundResource(R.drawable.drawable_flow_false);
                    }else {
                        tv.setBackgroundResource(R.drawable.drawable_flow_true);
                    }
                }
            }else {
                if (data.get(i).get("is_comment").equals("2")) {
                    tv.setBackgroundResource(R.drawable.drawable_flow_true);
                } else {
                    tv.setBackgroundResource(R.drawable.drawable_flow_false);
                }
            }
            tv.setTextColor(me.getResources().getColor(R.color.white));
            initEvents(tv, i);
            tv.setLayoutParams(layoutParams);
            flowLayout.addView(tv, layoutParams);
        }
    }

    private String labels = "";

    @OnClick({R.id.text_center, R.id.fl_context, R.id.text_parent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_center:
                finish();
                break;
            case R.id.fl_context:
                finish();
                break;
            case R.id.text_parent:
                //确认
                if (thiscount.equals("1")) {
                    finish();
                    return;
                }

                StringBuffer stringBuffer;
                if (data.size() != 0 || data != null) {
                    stringBuffer = new StringBuffer();
                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).get("is_comment").equals("2")) {
                            stringBuffer.append(data.get(i).get("label_id")).append(",");
                        }
                    }
                    labels = stringBuffer.toString();
                    labels = labels.substring(0, labels.length() - 1);
                }
                Log.d("addComment", "===============" + labels);
                presenter.addComment(token, member_id, labels);
                break;
            default:
        }
    }


    @Override
    public void flowFailed(String msg) {

    }

    @Override
    public void showLoading() {

    }

    public static void start(Context context, String member_id, String count, String sex) {
//       1 我的评价  2查看可以评价 和3查看不可以评价  sex 性别 （1男 2女）
        Bundle bundle = new Bundle();
        bundle.putString("member_id", member_id);
        bundle.putString("count", count);
        bundle.putString("sex", sex);
        Intent intent = new Intent(context, FlowDialog.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
}
